﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using SqlCon;



//cfbz 收费标志
//fybz 发药标志
//pybz 配药标志
//djybz 代煎药标志
namespace yfxt
{
    public partial class ms_mzfy : Form
    {
        //公共变量
        String StrSQL;
        SqlConnection conn;

        //公共变量 页面传值
        public string s_ymcz;

        public string S_ymcz
        {
            get { return s_ymcz; }
            set { this.s_ymcz = value; }
        }
        int i;//标志位

        public ms_mzfy()
        {
            InitializeComponent();
        }
        //手动去刷新页面 取得已经收费的 还没发药的处方
        private void button1_Click(object sender, EventArgs e)
        {

            StrSQL = "select cfhm,brxm from ms_cf01 where fybz='0' and cfbz='0'";//手动去刷新

            conn = new SqlConnection(SqlHelper.ConnectionStringLocalTransaction);

            if (conn.State != ConnectionState.Open) { conn.Open(); }//打开数据库连接


            SqlDataAdapter da = new SqlDataAdapter(StrSQL, conn);

            DataTable dt = new DataTable();

            da.Fill(dt);

            dataGridView1.DataSource = dt;
            dataGridView1.Columns[0].HeaderCell.Value = "处方号码";
            dataGridView1.Columns[1].HeaderCell.Value = "病人姓名";

        }

        //触发事件 选中某一行触发事件 也就是选中某一个处方 触发事件 显示处方的详细信息内容
        private void dataGridView1_SelectionChanged(object sender, EventArgs e)
        {

            if (dataGridView1.CurrentRow == null) return;

            string StrCfhm = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[0].Value.ToString();

            String StrSQL2 = "select yk_typk.ypmc as ypmc,yk_typk.yfgg as yfgg,yk_typk.ypdw as ypdw,ms_cf02.ypsl as ypsl,ms_cf02.ypyf," +
                                  " ms_cf02.ypdj as ypdj,yf_kcmx.ypsl as kcsl," +
                                  " (case ms_cf01.zfpb when '0' then '未发' when '1' then '已发' end) as zfpb," +
                                  " ms_cf01.cfhm,yk_typk.ypxh " +
                                  " from ms_cf01,ms_cf02,yk_typk,yf_kcmx " +
                                  " where ms_cf01.cfsb=ms_cf02.cfsb and ms_cf02.ypxh=yk_typk.ypxh and yf_kcmx.ypxh=yk_typk.ypxh and ms_cf01.cfhm=" + StrCfhm.ToString() +
                                  " group by yk_typk.ypmc,yk_typk.yfgg,yk_typk.ypdw,ms_cf02.ypsl,ms_cf01.zfpb," +
                                  " ms_cf02.ypyf,ms_cf02.ypdj,yf_kcmx.ypsl,ms_cf01.cfhm,yk_typk.ypxh";

            conn = new SqlConnection(SqlHelper.ConnectionStringLocalTransaction);

            if (conn.State != ConnectionState.Open) { conn.Open(); }

            SqlCommand cmd = new SqlCommand(StrSQL2, conn);


            SqlDataAdapter da = new SqlDataAdapter();

            da.SelectCommand = cmd;

            DataTable dt = new DataTable();

            da.Fill(dt);

            dataGridView2.DataSource = dt;

            dataGridView2.Columns[0].HeaderCell.Value = "药品名称";
            dataGridView2.Columns[1].HeaderCell.Value = "药品规格";
            dataGridView2.Columns[2].HeaderCell.Value = "药品单位";
            dataGridView2.Columns[3].HeaderCell.Value = "药品数量";
            dataGridView2.Columns[4].HeaderCell.Value = "药品用法";
            dataGridView2.Columns[5].HeaderCell.Value = "药品单价";
            dataGridView2.Columns[6].HeaderCell.Value = "库存数量";
            dataGridView2.Columns[7].HeaderCell.Value = "发药状态";
            dataGridView2.Columns[8].HeaderCell.Value = "处方号码";
            dataGridView2.Columns[9].HeaderCell.Value = "药品编码";



            //统计处方价格
            string StrCfhj = "select @cfhj=sum(ypsl*ypdj) from ms_cf01,ms_cf02 where ms_cf01.cfsb=ms_cf02.cfsb and ms_cf01.cfhm=@cfhm";

            using (SqlCommand cmd_Cfhj = new SqlCommand(StrCfhj, conn))
            {

                cmd_Cfhj.Parameters.AddWithValue("@cfhm", StrCfhm);

                SqlParameter sp = cmd_Cfhj.Parameters.Add("@cfhj", SqlDbType.Float, 20);
                sp.Direction = ParameterDirection.Output;

                cmd_Cfhj.ExecuteNonQuery();

                tb_cfhj.Text = cmd_Cfhj.Parameters["@cfhj"].Value.ToString();


                conn.Close();

            }

        }
        private void button4_Click(object sender, EventArgs e)//关闭窗口
        {
            Close();
        }

        private void ms_fy_Click(object sender, EventArgs e)///进行发药
        {
            string cfhm = tb_cfhm.Text.ToString(); //取得处方号码

            SqlParameter sp_01;

            conn = new SqlConnection(SqlHelper.ConnectionStringLocalTransaction);

            if (conn.State != ConnectionState.Open) { conn.Open(); }

            if (MessageBox.Show("您是否确定进行发药？？", "提示信息", MessageBoxButtons.YesNoCancel) == DialogResult.Yes)
            {
                for (int rowcount = 0; rowcount < dataGridView2.Rows.Count; rowcount++)
                {
                    //通过药品编号取得要发处方的单条药品数量//"select @ypsl=ypsl from V_cf01_cf02_typk_ypcd_cddz  where ypxh="
                    string s_select = "select @ypsl=ypsl from ms_cf01,ms_cf02,yk_typk,yk_ypcd,yk_cddz  " +
                                       "where ms_cf01.cfsb=ms_cf02.cfsb  " +
                                       "and ms_cf02.ypxh=yk_typk.ypxh  " +
                                       "and yk_ypcd.ypxh=yk_typk.ypxh  " +
                                       "and yk_ypcd.ypcd=yk_cddz.ypcd  " +
                                       "and yk_typk.ypxh=" + dataGridView2.Rows[rowcount].Cells["ypxh"].Value.ToString() + " ";

                    using (SqlCommand cmd_select = new SqlCommand(s_select, conn))
                    {
                        sp_01 = cmd_select.Parameters.Add("@ypsl", SqlDbType.Decimal, 40);
                        sp_01.Direction = ParameterDirection.Output;

                        cmd_select.ExecuteNonQuery();
                    }


                    //修改库存数量 进行发药 // 医生站可以显示药库库存数量 这样子才能开得出药品
                    string s_update = "update yf_kcmx set ypsl=ypsl-" + sp_01.Value.ToString() + " from yf_kcmx where ypxh="
                        + dataGridView2.Rows[rowcount].Cells["ypxh"].Value.ToString() + " ";
                    using (SqlCommand cmd_update = new SqlCommand(s_update, conn))
                    {
                        cmd_update.ExecuteNonQuery();

                    }
                    //显示发药进度
                }
            }


            //将已发处方打上标志 确认为已经是已发处方

            string s_yfcf = "update ms_cf01 set fybz=1  where cfhm= " + dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[0].Value.ToString() + " ";
            if (conn.State != ConnectionState.Open) { conn.Open(); }
            using (SqlCommand cmd_yfcf = new SqlCommand(s_yfcf, conn))
            {
                cmd_yfcf.ExecuteNonQuery();

                MessageBox.Show("发药成功！");
            }


            conn.Close();
        }

        private void tb_cfhm_KeyUp(object sender, KeyEventArgs e)//键盘输入处方号码搜索处方
        {

            StrSQL = "select cfhm,brxm from ms_cf01 where fybz='0'and cfbz='0'and cfhm=@cfhm";//搜索出来的处方必须是未发的处方 所以发药标志必须是0

            conn = new SqlConnection(SqlHelper.ConnectionStringLocalTransaction);

            string s_cfhm = tb_cfhm.Text.ToString();

            if (e.KeyCode == Keys.Enter)// 键盘回车事件
            {
                if (conn.State != ConnectionState.Open) { conn.Open(); }

                using (SqlConnection conn_01 = new SqlConnection(SqlHelper.ConnectionStringLocalTransaction))
                {
                    string s_select = "select cfhm from ms_cf01 where fybz='0'and cfbz='0'and cfhm='" + tb_cfhm.Text.ToString() + "' ";

                    if (conn_01.State != ConnectionState.Open) { conn_01.Open(); }

                    SqlCommand cmd_01 = new SqlCommand(s_select, conn_01);

                    SqlDataReader dr = cmd_01.ExecuteReader();

                    if (!dr.HasRows)
                    {
                        MessageBox.Show("没有这个处方号码的处方 ，请确认一下再重新输入");

                        return;
                    }
                }

                SqlCommand cmd = new SqlCommand(StrSQL, conn);

                cmd.Parameters.AddWithValue("@cfhm", s_cfhm);

                SqlDataAdapter da = new SqlDataAdapter();

                da.SelectCommand = cmd;

                DataTable dt = new DataTable();

                da.Fill(dt);

                dataGridView1.DataSource = dt;



            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            foreach (DataGridViewRow drw in dataGridView1.Rows)//显示每一行数
            {
                for (int t = 0; t < dataGridView1.Rows.Count; t++)
                    dataGridView1.Rows[t].HeaderCell.Value = Convert.ToString(t + 1);
            }
        }

        private void button2_Click(object sender, EventArgs e)//打印
        {
            if (dataGridView1.Rows.Count == 0) { return; }

            s_ymcz = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[0].Value.ToString();

            //MessageBox.Show(dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[0].Value.ToString());

            ms_fycfdy fycfdy = new ms_fycfdy();

            fycfdy.Owner = this;
            fycfdy.Show();
        }
        private void timer2_Tick(object sender, EventArgs e)
        {
            this.timer2.Interval = 100;
            this.timer2.Enabled = true;


            foreach (DataGridViewRow drw in dataGridView1.Rows)//显示每一行的行数
            {
                for (int t = 0; t < dataGridView1.Rows.Count; t++)
                    dataGridView1.Rows[t].HeaderCell.Value = Convert.ToString(t + 1);
            }
        }

        private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (tabControl1.SelectedTab == tabPage1)
            {
                i = 0;
                button1_Click(sender, e);


            }


            if (tabControl1.SelectedTab == tabPage2)//选中配药选帖 自动触发事件 然后搜出来数据
            {
                i = 1;
                string s = "select  cfhm,brxm  from ms_cf01 where pybz='0' and fybz='0' and cfbz='0'";//将配药发药的搜出来 pybz 为配药标志 0 ，不需配药的标志为1

                using (SqlConnection conn_01 = new SqlConnection(SqlHelper.ConnectionStringLocalTransaction))
                {
                    if (conn_01.State != ConnectionState.Open) { conn_01.Open(); }

                    SqlDataAdapter da = new SqlDataAdapter(s, conn_01);

                    DataTable dt = new DataTable();

                    da.Fill(dt);

                    dataGridView1.DataSource = dt;

                }

                if (dataGridView1.CurrentRow == null) return;
                string StrCfhm = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[0].Value.ToString();
                String StrSQL2 = "select yk_typk.ypmc as ypmc,yk_typk.yfgg as yfgg,yk_typk.ypdw as ypdw,ms_cf02.ypsl as ypsl,ms_cf02.ypyf," +
                                  " ms_cf02.ypdj as ypdj,yf_kcmx.ypsl as kcsl," +
                                  " (case ms_cf01.zfpb when '0' then '未发' when '1' then '已发' end) as zfpb," +
                                  " ms_cf01.cfhm,yk_typk.ypxh " +
                                  " from ms_cf01,ms_cf02,yk_typk,yf_kcmx " +
                                  " where ms_cf01.cfsb=ms_cf02.cfsb and ms_cf02.ypxh=yk_typk.ypxh and yf_kcmx.ypxh=yk_typk.ypxh and ms_cf01.cfhm=" + StrCfhm.ToString() +
                                  " group by yk_typk.ypmc,yk_typk.yfgg,yk_typk.ypdw,ms_cf02.ypsl,ms_cf01.zfpb," +
                                  " ms_cf02.ypyf,ms_cf02.ypdj,yf_kcmx.ypsl,ms_cf01.cfhm,yk_typk.ypxh";

                conn = new SqlConnection(SqlHelper.ConnectionStringLocalTransaction);



                if (conn.State != ConnectionState.Open) { conn.Open(); }


                SqlCommand cmd = new SqlCommand(StrSQL2, conn);

                SqlDataAdapter da_01 = new SqlDataAdapter();

                da_01.SelectCommand = cmd;

                DataTable dt_01 = new DataTable();

                da_01.Fill(dt_01);

                dataGridView3.DataSource = dt_01;

                dataGridView3.Columns[0].HeaderCell.Value = "药品名称";
                dataGridView3.Columns[1].HeaderCell.Value = "药品规格";
                dataGridView3.Columns[2].HeaderCell.Value = "药品单位";
                dataGridView3.Columns[3].HeaderCell.Value = "药品数量";
                dataGridView3.Columns[4].HeaderCell.Value = "药品用法";
                dataGridView3.Columns[5].HeaderCell.Value = "药品单价";
                dataGridView3.Columns[6].HeaderCell.Value = "库存数量";
                dataGridView3.Columns[7].HeaderCell.Value = "发药状态";
                dataGridView3.Columns[8].HeaderCell.Value = "处方号码";
                dataGridView3.Columns[9].HeaderCell.Value = "药品编码";



                string StrCfhj = "select @cfhj=sum(ypsl*ypdj) from ms_cf01,ms_cf02  where ms_cf01.cfsb=ms_cf02.cfsb and ms_cf01.cfhm=@cfhm";

                SqlCommand cmd_Cfhj = new SqlCommand(StrCfhj, conn);

                cmd_Cfhj.Parameters.AddWithValue("@cfhm", StrCfhm);

                SqlParameter sp = cmd_Cfhj.Parameters.Add("@cfhj", SqlDbType.Float, 20);
                sp.Direction = ParameterDirection.Output;

                cmd_Cfhj.ExecuteNonQuery();
                textBox3.Text = cmd_Cfhj.Parameters["@cfhj"].Value.ToString();


                conn.Close();


            }

        }

        private void ms_mzfy_Load(object sender, EventArgs e)
        {
            //string s = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[0].ToString();



            //找到未发药的处方 fybz 发药标志  0为未发药 1为发药 2为退药 cfbz为处方收费标志 0表示已经收费 1表示未收费 2为退费
            StrSQL = "select cfhm,brxm from ms_cf01 where fybz='0'and cfbz=0";

            conn = new SqlConnection(SqlHelper.ConnectionStringLocalTransaction);



            if (conn.State != ConnectionState.Open) { conn.Open(); }


            SqlDataAdapter da = new SqlDataAdapter(StrSQL, conn);

            DataTable dt = new DataTable();

            da.Fill(dt);

            dataGridView1.DataSource = dt;


            foreach (DataGridViewRow drw in dataGridView1.Rows)//显示每一行的行数
            {
                for (int t = 0; t < dataGridView1.Rows.Count; t++)
                    dataGridView1.Rows[t].HeaderCell.Value = Convert.ToString(t + 1);
            }

            dataGridView1.Refresh();
            dataGridView1.Columns[0].HeaderCell.Value = "处方号码";
            dataGridView1.Columns[1].HeaderCell.Value = "病人姓名";



            label4.Text = "窗口一";

            label6.Text = "管理员";

            label8.Text = "管理员";



            //为timer定时刷新

            this.timer1.Interval = 100;
            this.timer1.Enabled = true;

            timer2_Tick(sender, e);

        }

    }
}
